import 'package:flutter/material.dart';
import 'package:my_flutter/store/Book.dart';
import 'package:my_flutter/store/LoginData.dart';
import 'package:my_flutter/store/Person.dart';
import 'package:provider/provider.dart';

/// 1, Store.init
/// 2, Store.setContext(context)
/// 3, Store.of<T>().mData = t
/// 4, var value = Store.of<T>().mData;


///全局的状态管理
class Store {
  static BuildContext? _context;
  static init(child) {
    return MultiProvider(
      child: child,
      providers: [
        //无监听效果的
        Provider.value(value: Book()),
        Provider(create: (context)=> LoginStore()),
        //有监听效果的
        ChangeNotifierProvider(create: (context)=> Person())
      ],
    );
  }
  static setContext(BuildContext context) {
    Store._context ??= context;
    return context;
  }
  //获取store<T>
  static T of<T>({ bool listen = false}) {
    return Provider.of<T>(_context!, listen: listen);
  }
}

